package com.yanqu.road.server.cmd.activity.greattrade;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.GreatTradeProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.greattrade.GreatTradeModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.greattrade.GreatTradeMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GREAT_TRADE_INDUSTRY_SOLD, desc = "出售产业")
public class GreatTradeIndustrySoldCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInTime(GreatTradeMgr.getActivityInfo()) || GreatTradeMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        GreatTradeProto.GreatTradeIndustrySoldReqMsg reqMsg = GreatTradeProto.GreatTradeIndustrySoldReqMsg.parseFrom(packet.getBytes());
        int type = reqMsg.getType();
        int num = reqMsg.getNum();

        if(num <= 0){
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        ret = player.getModule(GreatTradeModule.class).soldIndustry(type, num);
    }

    @Override
    public void sendData(GamePlayer player) {

        if(ret != 0){
            player.sendPacket(Protocol.U_GREAT_TRADE_INDUSTRY_SOLD, GreatTradeProto.GreatTradeIndustrySoldRespMsg.newBuilder().setRet(ret));
        }

    }
}
